Public Class frmMain

    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim auDetails As IO.StreamReader
        Dim line As String
        Dim version As String
        Dim fileCnt As Integer
        Dim i As Integer
        Dim fileParts() As String
        Dim ernLocation As String = "c:\program files\ERN"
        Dim ernVersion As String = "0.0.0.1"
        Dim ernAUId As String = "0"
        Dim ernClientID As String = ""
        Dim batchProc As Process
        Dim auLog As IO.StreamWriter

        If System.Environment.GetCommandLineArgs().Length > 1 Then
            For i = 1 To Environment.GetCommandLineArgs().Length - 1
                If Split(System.Environment.GetCommandLineArgs()(i), "=")(0) = "-l" Then
                    ernLocation = Split(System.Environment.GetCommandLineArgs()(i), "=")(1)
                ElseIf Split(System.Environment.GetCommandLineArgs()(i), "=")(0) = "-v" Then
                    ernVersion = Split(System.Environment.GetCommandLineArgs()(i), "=")(1)
                ElseIf Split(System.Environment.GetCommandLineArgs()(i), "=")(0) = "-i" Then
                    ernAUId = Split(System.Environment.GetCommandLineArgs()(i), "=")(1)
                ElseIf Split(System.Environment.GetCommandLineArgs()(i), "=")(0) = "-p" Then
                    ernClientID = Split(System.Environment.GetCommandLineArgs()(i), "=")(1)
                End If
            Next
        Else
            If Not IO.Directory.Exists(ernLocation) Then IO.Directory.CreateDirectory(ernLocation)
        End If

        'stop the running ERN client
        If ernClientID <> "" Then
            Diagnostics.Process.GetProcessById(ernClientID).Kill()
            Threading.Thread.Sleep(1000)
        End If

        'format of audetails.txt
        'update version
        'overwrite
        'filename
        'location
        ''''''''''''''''''''''''''''''
        'parse audetails.txt
        auDetails = IO.File.OpenText(IO.Path.GetDirectoryName(Application.ExecutablePath) & "\auDetails.txt")
        line = auDetails.ReadLine
        version = Split(line, ":")(1)
        line = auDetails.ReadLine
        fileCnt = Split(line, ":")(1)

        For i = 0 To fileCnt - 1
            line = auDetails.ReadLine
            fileParts = Split(line, "::")
            fileParts(3) = fileParts(3).ToUpper().Replace("%ERN%", ernLocation)
            IO.File.Copy(Application.StartupPath & "\" & fileParts(2), fileParts(3) & "\" & fileParts(2), fileParts(1))
        Next

        'run batch script
        If IO.File.Exists(Application.StartupPath & "\aubatch.bat") Then
            batchProc = Diagnostics.Process.Start(Application.StartupPath & "\aubatch.bat")
            batchProc.WaitForExit()
        End If

        'update aulog
        auLog = New IO.StreamWriter(ernLocation & "\au.log", True)
        auLog.WriteLine("1::" & Date.Now & "::" & ernVersion & "::" & ernAUId & "::" & "0" & "::" & "Success!")
        auLog.Close()

        'relaunch ERN
        'MsgBox(ernLocation & "\ern_client.exe")
        Diagnostics.Process.Start(ernLocation & "\ern_client.exe")

        Me.Close()
    End Sub
End Class
